import { createPinia, defineStore } from 'pinia'
import { ref } from 'vue'
import { bookAPI, customerAPI, productAPI } from '../api'

const pinia = createPinia()

// Pinia Store - 定义数据管理store
export const useDataStore = defineStore('data', () => {
  // 状态定义
  const customers = ref([])
  const books = ref([])
  const products = ref([])
  const loading = ref(false)

  // Actions
  const fetchCustomers = async (keyword = '') => {
    loading.value = true
    try {
      const response = await customerAPI.getAll(keyword)
      customers.value = response.data
    } catch (error) {
      console.error('获取客户数据失败:', error)
    } finally {
      loading.value = false
    }
  }

  const fetchBooks = async (keyword = '') => {
    loading.value = true
    try {
      const response = await bookAPI.getAll(keyword)
      books.value = response.data
    } catch (error) {
      console.error('获取图书数据失败:', error)
    } finally {
      loading.value = false
    }
  }

  const fetchProducts = async (keyword = '') => {
    loading.value = true
    try {
      const response = await productAPI.getAll(keyword)
      products.value = response.data
    } catch (error) {
      console.error('获取文创产品数据失败:', error)
    } finally {
      loading.value = false
    }
  }

  return {
    // 状态
    customers,
    books,
    products,
    loading,
    // Actions
    fetchCustomers,
    fetchBooks,
    fetchProducts
  }
})

export default pinia 